/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = syncmodule_analysis.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2015/03/11 by guojq                                */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <syncmodule/syncmodule_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/syncmodule/config.h"

extern int syncmodule_analysis(Conntrack *p_st_conn
                      , Apm_packet *p_st_packet
                      , Frhash_header *p_st_hash_report);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;

    i_ret = syncmodule_analysis(NULL, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;

    i_ret = syncmodule_analysis(p_st_conn, NULL, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;

    i_ret = syncmodule_analysis(p_st_conn, &st_packet, NULL);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

static void case5(void)
{
    int i_ret = 0;
    //int i_loop = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 20;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

static void case6(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 20;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.i_direction = FR_DIRECT_SRV2CLIENT;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.src_port = 8000;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.dst_port = 80;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 205;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

static void case7(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;
    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 20;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.i_direction = FR_DIRECT_SRV2CLIENT;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.src_port = 8000;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.dst_port = 80;
    st_packet.st_timestamp.tv_sec = 1417060360;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.p_payload = (char *)"test";
    st_packet.ui_payload_len = 205;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.src_port = 8000;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.dst_port = 80;
    st_packet.st_timestamp.tv_sec = 1417060361;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 155;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

static void case8(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_FIN;
    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

static void case9(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet st_packet;
    Frhash_header st_hash_report;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_conn->ip_proto = IPPROTO_TCP;
    p_st_conn->src_ip = iptools_parse_ipaddr("172.16.0.1");
    p_st_conn->src_port = 80;
    p_st_conn->dst_ip = iptools_parse_ipaddr("172.16.0.15");
    p_st_conn->dst_port = 8000;

    frhash2_create(&st_hash_report, 10);

    memset(&st_packet, 0x00, sizeof(Apm_packet));
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    st_packet.src_ip = iptools_parse_ipaddr("172.16.0.1");
    st_packet.src_port = 80;
    st_packet.dst_ip = iptools_parse_ipaddr("172.16.0.15");
    st_packet.dst_port = 8000;
    st_packet.st_timestamp.tv_sec = 1417060359;
    st_packet.i_l7_proto = IPPROTO_TCP;
    st_packet.em_analyz_cmd = FRANALYZ_CMD_SYN;

    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.em_analyz_cmd = FRANALYZ_CMD_DATA;
    st_packet.ui_payload_len = 20;
    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    st_packet.em_analyz_cmd = FRANALYZ_CMD_FIN;
    st_packet.ui_payload_len = 20;
    syncmodule_analysis(p_st_conn, &st_packet, &st_hash_report);

    frhash2_free(&st_hash_report, NULL);
    frfree(p_st_conn);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    return;
}

int add_suite_syncmodule_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" syncmodule_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL ||
        CU_add_test(pSuite, "case9", case9) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
